import { httpGet, httpPost, httpUpload, httpGetBlob } from '@/utils/http'
import type { ChatResponse, RoleDetailResponse, ChatHistoryResponse, RoleListResponse } from './interface'

// 基础接口
export const banner = () => httpGet('/banner')

// 角色相关接口
export const characterInfo = {
  // 角色查询/列表接口（直连接口，无缓存）
  list: (params: { keyword?: string; category?: string; pageNum?: number; pageSize?: number }) => {
    return httpGet<RoleListResponse>('/api/characterInfo/list', params)
  }
  // 角色详情接口
  ,detail: (id: number | string) => httpGet<RoleDetailResponse>(`/api/characterInfo/${id}`)

  // AI帮助设计角色接口(已弃用，改为多个接口，分别调用)
  ,generateRole: (data: { name: string; prompt: string }) => 
    httpPost<any>('/api/characterInfo/createSmart', data)
  // 创建自定义角色接口（加入 intro 字段）
  ,create: (data: { name: string; prompt: string; greeting: string; intro?: string; voice: string; image: string }) => 
    httpPost<any>('/api/characterInfo/create', data)
  // 我的收藏列表
  ,favorites: () => httpGet<any>('/api/characterInfo/favorites')
  // 收藏角色
  ,favorite: (id: number | string) => 
    httpPost<any>(`/api/characterInfo/favorite/${id}`)
  // 取消收藏角色
  ,unfavorite: (id: number | string) => 
    httpPost<any>(`/api/characterInfo/unfavorite/${id}`)
  // 根据设定生成招呼语
  ,assistGreeting: (data: { name: string; prompt: string }) =>
    httpPost<any>('/api/characterInfo/assist/greeting', data)
  // 根据设定生成形象(头像)，返回多张图，前端默认取第一张
  ,assistAvatar: (data: { name: string; prompt: string }) =>
    httpPost<any>('/api/characterInfo/assist/avatar', data)
  // 根据设定生成角色简介
  ,assistIntro: (data: { name: string; prompt: string }) =>
    httpPost<any>('/api/characterInfo/assist/intro', data)
  // 自定义头像上传
  ,uploadAvatar: (file: File) => {
    const formData = new FormData()
    formData.append('file', file)
    // 使用通用上传入口
    return httpUpload<any>('/api/file/upload', formData)
  }
  // 获取可选音色列表
  ,voices: () => httpGet<any>('/api/characterInfo/voices')
  // 播放招呼语音频（返回 wav Blob）- 已弃用
  ,voicePlay: (id: string | number) => httpGetBlob(`/api/characterInfo/voicePlay/${id}`)
  // 角色招呼语流式播放（SSE流）
  ,voiceStreamPlay: (id: string | number) => `/api/characterInfo/voiceStreamPlay/${id}`
  // 上传音频文件并生成音色
  ,uploadVoice: (file: File) => {
    const formData = new FormData()
    formData.append('file', file)
    // 生成8位随机字符串作为file参数
    const randomString = Math.random().toString(36).substring(2, 10)
    return httpUpload<any>(`/api/characterInfo/voice/upload?file=${randomString}`, formData)
  }
}

// 角色聊天相关接口
export const roleChat = {
  // 发送消息到角色聊天 (使用GET请求，参数通过URL传递)
  sendMessage: (userInput: string, chatId: string) => 
    // 按后端要求，userInput 不做 encode 处理，直接透传
    httpGet<ChatResponse>(`/api-ai/roleChat/streamChat?userInput=${userInput}&chatId=${chatId}`),
  
  
}

// 聊天记录相关接口
export const chatHistory = {
  // 根据角色ID获取聊天记录
  byCharacter: (characterId: number | string) => httpGet<ChatHistoryResponse>(`/api/chatHistory/byCharacter/${characterId}`)
  // 获取对话过的角色列表
  ,firstList: () => httpGet<any>('/api/chatHistory/firstList')
}

